package www.nomina.controller.bean.sat;

import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.Locale;
import java.util.TimeZone;

import javax.xml.bind.DatatypeConverter;
import javax.xml.datatype.DatatypeConfigurationException;
import javax.xml.datatype.DatatypeFactory;
import javax.xml.datatype.XMLGregorianCalendar;

public class GetDate {

	public static XMLGregorianCalendar novXML(){
		XMLGregorianCalendar xmlCal=null;
		
		
		TimeZone tz = TimeZone.getTimeZone("America/Monterrey");
		Locale locale = new Locale("ES", "MX");
		GregorianCalendar now = (GregorianCalendar) GregorianCalendar.getInstance(tz, locale);
		now.clear(14);
		try{
			
			xmlCal=DatatypeFactory.newInstance().newXMLGregorianCalendar(now);
			
		}catch(DatatypeConfigurationException e){
			
		}
			return xmlCal;
		
	}
	public static Calendar getCalendar(int year, int month, int day, int hour, int min, int sec){
		GregorianCalendar fechaAuth = null;
		TimeZone tz = TimeZone.getTimeZone("America/Monterrey");
		Locale locale = new Locale("ES", "MX");
		GregorianCalendar now = (GregorianCalendar) GregorianCalendar.getInstance(tz, locale);
		fechaAuth.set(year,month,day,hour,min,sec);
		fechaAuth.clear(15);
		fechaAuth.clear(14);
				
		return fechaAuth;
		
	}
	public static Calendar parseDateTime(String lexicalXSDDateTime){
		if(lexicalXSDDateTime == null){
			return null;
		}
		Calendar cal= DatatypeConverter.parseDateTime(lexicalXSDDateTime);
		cal.clear(14);
		return cal;
	}
	public static String printDateTime(Calendar val){
		if(val==null){
			return null;
		}
		val.clear(14);
		return DatatypeConverter.printDate(val);
	}
	public static String printDateTimeNoTZ(Calendar val){
		String res = null;
		if(val== null){
			return null;
			
		}
		val.clear(14);
		res=DatatypeConverter.printDateTime(val);
		res=res.substring(0,19);
		return res;
		
	}
	public static String printDateTimeNoTZZ(Calendar val){
		String res = null;
		if(val == null){
			return null;
			
		}
		val.clear(14);
		res= DatatypeConverter.printDateTime(val);
		res= res.substring(0,19);
		return res;
	}
	public static String printDateNoTZ(Calendar val){
		String res = null;
		if(val==null){
			return null;
		}
		val.clear(14);
		res=DatatypeConverter.printDate(val);
		res= res.substring(0,19);
		return res;
	}
	public static String printBaseString64Binary(byte[] val){
		if(val == null){
			return null;
		}
		String converted = DatatypeConverter.printBase64Binary(val);
		String result = converted;
		int size = converted.length();
		if(size > 76){
			String segmented="";
			int iniIndex = 0;
			int finIndex=76;
			while(finIndex <= size){
				segmented= segmented + converted.substring(iniIndex, finIndex)+ "\n";
				iniIndex += 76;
				finIndex += 76;
						
			}
			segmented = segmented+converted.substring(iniIndex, size);
		}
		return result;
	}
}
